package com.zhxh.nzpos.api;

import java.util.Vector;

import org.ksoap2.serialization.SoapObject;

import com.zhxh.nzpos.api.exception.AuthenticationException;
import com.zhxh.nzpos.api.exception.AuthorizationException;
import com.zhxh.nzpos.api.exception.CommunicationException;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.model.Goods;


public class ConnectorGoods {

	private KSoapExecutor soap;
	
	public ConnectorGoods(){
		
		soap = new KSoapExecutor();
	}
	
	public Goods[] getGoods()
	        throws CommunicationException, AuthorizationException,
	        AuthenticationException {

		SoapObject getProjects = SoapObjectBuilder.start()
		        .withMethod("GetAllGoodsList").withProperty("key", "3e6f5018cd")
		        .build();

		@SuppressWarnings("unchecked")
		SoapObject vc = null;
		try {
			vc = (SoapObject) soap.execute(getProjects);
			System.out.println("#####################vc="+vc);
		} catch (org.apache.http.auth.AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (vc == null) return null;
		int count = vc.getPropertyCount();
		Goods[] goodsz = new Goods[count];

		SoapObject p;
		for (int i = 0; i < count; ++i) {
			p = (SoapObject) vc.getProperty(i);

			// Creating new goods with all fields
			Goods goods = new Goods();
			goods.setCategoryId(p.getPropertySafelyAsString("CategoryID"));
			goods.setId(  p.getPropertySafelyAsString("ID"));
			goods.setCode( p.getPropertySafelyAsString("GoodsBarCode"));
			goods.setName( p.getPropertySafelyAsString("Name"));
			goods.setUnits1( p.getPropertySafelyAsString("Units"));
			goods.setSpecification( p.getPropertySafelyAsString("Specification"));
			String price = p.getPropertySafelyAsString("Price");
			if(price != null)
			goods.setPrice1( Float.parseFloat(price));
			String ponits = p.getPropertySafelyAsString("Ponits");
			if(ponits != null && ponits.trim().length() != 0)
			goods.setPonits( Integer.parseInt(ponits));
		//	goods.setNote( p.getPropertySafelyAsString("Note"));
			goods.setCategoryName("");
			goods.setNote("");
			goods.setUnits2("");
			goods.setType(0);
			goodsz[i] =goods; 
			
		}
		return goodsz;
	}
	public Category[] getAllCategories()
	        throws CommunicationException, AuthorizationException,
	        AuthenticationException {

		SoapObject getProjects = SoapObjectBuilder.start()
		        .withMethod("GoodsCategoryList").withProperty("Key",  SoapObjectBuilder.KEY)
		        .build();

		@SuppressWarnings("unchecked")
		SoapObject vc = null;
		try {
			vc = (SoapObject) soap.execute(getProjects);
			System.out.println("#####################vc="+vc);
		} catch (org.apache.http.auth.AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (vc == null) return null;

		SoapObject p;
		int count = vc.getPropertyCount();
		Category[] categorys = new Category[count];

		for (int i = 0; i < count; ++i) {
			p = (SoapObject) vc.getProperty(i);

			// Creating new goods with all fields
			Category category = new Category();
			category.setId(p.getPropertySafelyAsString("ID"));
			category.setCategory(p.getPropertySafelyAsString("Category"));

			categorys[i] = category;

		}
		return categorys;
	}
}
